package ee.maoinvest.frontend.controller.command.statisticsquerycommands;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.model.data.RegularReading;
import ee.maoinvest.backend.services.RegularReadingsService;
import ee.maoinvest.backend.services.implementations.RegularReadingsServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.statisticsqueryforms.GetHelpFindingRegularReadingIDWantedFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.StatisticsQueryFormsRequestProcessor;

public class GetHelpFindingRegularReadingIDWantedCommand implements Command
{
	private Logger logger = Logger.getLogger(GetHelpFindingRegularReadingIDWantedCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		try
		{
			StatisticsQueryFormsRequestProcessor statisticsQueryFormsRequestProcessor = new StatisticsQueryFormsRequestProcessor();
			GetHelpFindingRegularReadingIDWantedFormConverter getHelpFindingRegularReadingIDWantedFormConverter = null;
			
			getHelpFindingRegularReadingIDWantedFormConverter = 
					statisticsQueryFormsRequestProcessor.fillGetHelpFindingRegularReadingIDWantedFormConverterWithRequestData(request);
			getHelpFindingRegularReadingIDWantedFormConverter.convertToModelData();
			
			boolean dataIsOKAfterFormatting = getHelpFindingRegularReadingIDWantedFormConverter.getIsDataAfterFormattingOK();
			
			if (dataIsOKAfterFormatting)
			{
				RegularReadingsService regularReadingsServiceImpl = new RegularReadingsServiceImpl();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Date minDate = new Date(formatter.parse(getHelpFindingRegularReadingIDWantedFormConverter.getMinDate()).getTime());
				Date maxDate = new Date(formatter.parse(getHelpFindingRegularReadingIDWantedFormConverter.getMaxDate()).getTime());
				List<RegularReading> listOfRegularReadingsTakenBetweenGivenDates = regularReadingsServiceImpl.getListOfRegularReadingsTakenBetweenGivenDates(
						minDate, maxDate);
				request.setAttribute("listOfRegularReadingsTakenBetweenGivenDates", listOfRegularReadingsTakenBetweenGivenDates);
				operationResult = OPERATION_SUCCESSFUL;
				
			}
			else
			{
				operationResult = OPERATION_UNSUCCESSFUL;
			}
			
			request.setAttribute("getHelpFindingRegularReadingIDWantedFormConverter", 
					getHelpFindingRegularReadingIDWantedFormConverter);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}